home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************
- "misc.c"
-
- by John A. Love, III [ Washington Apple Pi Users' Group]
-
- using Symantec's "THINK C", v 5.00
- *********************************************************/
-
-
- #ifndef __SOUND__
- #include <Sound.h>
- #endif
-
- #ifndef _H_math
- #include <math.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #include <CType.h>
-
- #include "protos"
-
- #include "globals.h"
- #include "extern.h"
-
- #include "floatingWindow.h"
-
-
- void FatalSystemCrash (void);
- void MyMoreMasters (short numMasterPtrs);
- TrapType GetTrapType (short theTrap);
- short GetTrapNum (short theTrap);
- short NumToolboxTraps (void);
-
-
- short SysEnvironsVersion = 1;
- SysEnvRec theWorld;
-
-
-
-
- /* ====================================
- No further explanation is required :
- ==================================== */
-
- void FatalSystemCrash (void) {
-
- ExitToShell();
-
- } /* FatalSystemCrash */
-
-
-
- void MyMoreMasters (short numMasterPtrs) {
- /* See Technical Note #53: */
-
- short oldMoreMast;
- THz zone;
-
-
- zone = GetZone();
-
- oldMoreMast = zone->moreMast;
- zone->moreMast = numMasterPtrs;
- MoreMasters(); /* Calls itself "moreMast" times. */
- zone->moreMast = oldMoreMast;
- if (MemError()) ExitToShell();
-
- } /* MyMoreMasters */
-
-
-
- void InitManagers (void) {
-
- MaxApplZone();
- MyMoreMasters(15);
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(&FatalSystemCrash);
- ;
- FlushEvents(everyEvent, 0);
- InitCursor();
-
- } /* InitManagers */
-
-
-
- /* ================================================================================
- Test for the presence of a Mac with Color QuickDraw and a Color Monitor that the
- user has set to Color via the Control Panel or using the "Switch-A-Roo" FKEY.
-
- Return the color depth:
- ================================================================================ */
-
- Boolean TestForColor (short *pixelDepth) {
-
- OSErr whoCares; /* Compiler's "glue" for _SysEnvirons fills-in
- all fields EXCEPT systemVersion. */
-
- whoCares = SysEnvirons(SysEnvironsVersion, &theWorld);
- if (theWorld.hasColorQD)
- {
- *pixelDepth = (**((*GetGDevice())->gdPMap)).pixelSize;
- return(true);
- }
- else
- {
- *pixelDepth = 1;
- return(false);
- };
-
- } /* TestForColor */
-
-
-
- Boolean System7isUp (void) {
-
- OSErr itDoesMatter;
-
-
- itDoesMatter = SysEnvirons(SysEnvironsVersion, &theWorld);
- if ((itDoesMatter == noErr) && (theWorld.systemVersion >= 0x0700)) return (true);
- else return (false);
-
- } /* System7isUp */
-
-
-
- void System7Braggart (void) {
-
- short kSystem7 = 777;
- long onHeap = -1;
- SysEnvRec theWorld;
- OSErr itDoesMatter;
- GrafPtr oldPort;
- WindowPtr window;
- PicHandle myPic;
- Rect wpRect, picRect;
- long finalTicks;
-
-
- itDoesMatter = SysEnvirons(SysEnvironsVersion, &theWorld);
- if ((itDoesMatter == noErr) && (theWorld.systemVersion >= 0x0700))
- {
- GetPort(&oldPort);
-
- window = GetNewWindow(kSystem7, nil, (WindowPtr)onHeap);
- if (window != nil)
- {
- SetPort(window);
- wpRect = window->portRect;
- ClipRect(&wpRect);
- myPic = GetPicture(kSystem7);
-
- if (myPic != nil)
- {
- picRect = wpRect;
- InsetRect(&picRect, 1, 1);
- DrawPicture(myPic, &picRect); // ... so it's centered.
- Delay(60, &finalTicks);
- InvertRect(&wpRect);
- Delay(120, &finalTicks);
- KillPicture(myPic);
- } /* Got a picture */
-
- DisposeWindow(window);
- } /* Got a window */
-
- SetPort(oldPort);
-
- } /* Got System 7 */
-
- } /* System7Braggart */
-
-
-
- /* ===================
- A short-cut or two:
- =================== */
-
- void LocalGlobal (Rect *r) {
-
- LocalToGlobal(&topLeft(*r));
- LocalToGlobal(&botRight(*r));
-
- } /* LocalGlobal */
-
-
-
- void GlobalLocal (Rect *r) {
-
- GlobalToLocal(&topLeft(*r));
- GlobalToLocal(&botRight(*r));
-
- } /* GlobalLocal */
-
-
-
- short Max (short a, short b) {
-
- if (a >= b) return(a);
- else return(b);
-
- } /* Max */
-
-
-
- short Min (short a, short b) {
-
- if (a <= b) return(a);
- else return(b);
-
- } /* Min */
-
-
-
- double xPOWERy (double x, double y) {
-
- double times, temp;
-
-
- temp = x;
- for (times = y-1; times; times--) x *= temp;
- return (x);
-
- } /* xPOWERy */
-
-
-
- /* ====================================
- Common to the routines that follow:
- Reference: IM, Volume VI, Chapter 3:
- ==================================== */
-
- TrapType GetTrapType (short theTrap) {
-
- short TrapMask = 0x0800; /* Tests Bit #11. */
-
-
- if (theTrap & TrapMask) return(ToolTrap);
- else return(OSTrap);
-
- } /* GetTrapType */
-
-
-
- short GetTrapNum (short theTrap) {
-
- short ToolMask = 0x01FF;
- short OSMask = 0x00FF;
-
-
- if (GetTrapType(theTrap) == ToolTrap) return(theTrap & ToolMask);
- else return(theTrap & OSMask);
-
- } /* GetTrapNum */
-
-
-
- short NumToolboxTraps (void) {
-
- /* #define _InitGraf = 0xA86E; */
- short _Magic = 0xAA6E;
-
-
- if (NGetTrapAddress(GetTrapNum(_InitGraf), GetTrapType(_InitGraf)) ==
- NGetTrapAddress(GetTrapNum(_Magic), GetTrapType(_Magic))) return(0x0200);
- else return(0x0400);
-
- } /* NumToolboxTraps */
-
-
-
- Boolean TrapAvailable (short theTrap) {
-
- short trapNum, tempINT;
- TrapType tType;
-
-
- trapNum = GetTrapNum(theTrap);
- tType = GetTrapType(theTrap);
-
- if (tType == ToolTrap) {
- tempINT = theTrap ^ 0xA800;
- if (tempINT >= NumToolboxTraps()) trapNum = GetTrapNum(_Unimplemented);
- } /* a ToolTrap */
-
- return(NGetTrapAddress(trapNum, tType) !=
- NGetTrapAddress(GetTrapNum(_Unimplemented), GetTrapType(_Unimplemented)));
-
- } /* TrapAvailable */
-
-
-
- /* ==============================================
- Now, let's put this new fangled stuff to work:
- ============================================== */
-
- Boolean WNEisImplemented (void) {
-
- return(TrapAvailable(_WaitNextEvent));
-
- } /* WNEisImplemented */
-
-
-
- /* ===============
- Play it, Sam !!
- =============== */
-
- void PlaySound (char *mySound) {
-
- Handle sndHandle;
- OSErr discardError;
- unsigned char *pSound;
-
-
- if (TrapAvailable(_SndPlay))
- {
- if (!isPString(mySound)) pSound = CtoPstr(mySound);
- else pSound = (unsigned char*)mySound;
-
- sndHandle = GetNamedResource('snd ', pSound);
- if (sndHandle)
- {
- discardError = SndPlay (nil, sndHandle, false);
- ReleaseResource(sndHandle);
- }
- } /* _SndPlay is implemented */
-
- } /* PlaySound */
-
-
-
- long GetStripAddressMask (void) {
- /* Adapted from Macintosh Tech Note #213 */
-
- long gLo3Bytes = 0x00FFFFFF;
- /* #define _StripAddress = 0xA055; */
-
- long localBiggee;
-
-
- if (TrapAvailable(_StripAddress))
- {
- localBiggee = 0xFFFFFFFF;
- return((long)StripAddress((Ptr)&localBiggee));
- }
- else return(gLo3Bytes);
-
- } /* GetStripAddressMask */
-
-
-
- Ptr QuickStrip (long *myPtr) {
-
- return((Ptr)((long)myPtr & gStripAddressMask));
-
- } /* QuickStrip */
-
-
-
- Boolean isPString (char *myString) {
- /*
- ** If FALSE, then it's a C string.
- **
- ** NOT a totally general approach since we assume that
- ** every character in the actual string is printable:
- */
-
- short len, maxLen = 255;
- register short i;
- Boolean result, PString = true, CString = false;
-
-
- len = *myString;
-
- if ( !isprint(len) ) result = PString;
- else
- {
- for (i = 1; i <= len; ++i)
- if ( !isprint(myString[i]) ) break;
- /*
- ** If every character is printable, break caused by the
- ** only remaining unprintable character, namely, the C string's
- ** NULL terminator. If no break happens, i = len + 1:
- */
- if (i <= len) result = CString;
- else
- {
- for (; i <= maxLen; ++i)
- if ( !isprint(myString[i]) ) break;
- result = (i > maxLen); // true: Pascal string with length = len.
- // false: C string with its NULL terminator.
- }
- }
-
- return (result);
-
- } /* isPString */
-
-
-
- void pStrCat (Str255 s1, Str255 s2, Str255 s3) {
-
- short len1, len2;
-
-
- len1 = *s1++;
- len2 = *s2++;
-
- *s3++ = len1 + len2;
- while (--len1 >= 0) *s3++ = *s1++;
- while (--len2 >= 0) *s3++ = *s2++;
-
- } /* pStrCat */
-
-
-
- void pStrCopy (Str255 s1, Str255 s2) {
-
- short len;
-
-
- len = *s2++ = *s1++;
- /*
- len = *s1++;
- *s2++ = len;
- */
-
- while (--len >= 0) *s2++ = *s1++;
-
- } /* pStrCopy */
-
-
-
- void DoSpiffyIris (void) {
-
- short bragWindowID = 999, IACS = 3001;
- WindowPtr bragging;
- PicHandle logoPicHdl, centeredPic;
- short tempX, tempY, maskPercent;
- Rect windRect, pictRect, bragRect, irisRect;
- RgnHandle irisRgn;
- OffScreenRecHdl bragOSHdl;
- long finalTicks;
-
-
- SetCursor( *GetCursor(watchCursor) );
-
- if (!gMac2) bragging = GetNewWindow (bragWindowID, nil, (WindowPtr)-1);
- else bragging = GetNewCWindow (bragWindowID, nil, (WindowPtr)-1);
- if (bragging == nil) {
- SysBeep(10);
- return;
- }
-
- SetPort(bragging);
- TShowWindow(bragging);
- ;
- logoPicHdl = GetPicture(IACS);
- if (logoPicHdl == nil) {
- SysBeep(10);
- return;
- }
-
- bragRect = bragging->portRect;
-
- /* if (ScrollVert(bragging) != nil)
- bragRect.right = (bragRect.right + frame) - scrollWidth;
- if (ScrollHoriz(bragging) != nil)
- bragRect.bottom = (bragRect.bottom + frame) - scrollHeight; */
-
- pictRect = (**logoPicHdl).picFrame; // Center the PICT ...
- CenterRects(&pictRect, &bragRect);
-
- // Make a new PICT, this one centered & clipped to my portRect:
- ClipRect(&bragRect);
- centeredPic = OpenPicture(&bragRect);
- ClipRect(&bragRect);
- EraseRect(&bragRect);
- InvertRect(&bragRect);
- DrawPicture(logoPicHdl, &pictRect);
- ClosePicture();
- ;
- MoveHHi(centeredPic);
- HLock(centeredPic);
- ReleaseResource(logoPicHdl); /* Kiss the original good-bye !! */
-
- /* -------------------------------------------
- ** Place my PICTure into an off screen BitMap.
- ** ------------------------------------------- */
-
- /* Local window in, local screen back: */
- bragOSHdl = CreateOffScreen(&bragRect);
- if ( ((*bragOSHdl)->CreateOffScreenError) != noErr)
- goto byebye;
- {
- ClipRect(&bragRect); /* Draw off-screen ... */
- EraseRect(&bragRect); /* Eliminate all stray matter. */
- DrawPicture(centeredPic, &bragRect);
- }
- ToOnScreen(bragOSHdl); /* Back to "Square 1". */
-
- windRect = (**bragOSHdl).drawingRect;
- ClipRect(&windRect);
- // InvertRect(&windRect); /* Part of centeredPic. */
-
- BackColor(whiteColor);
- ForeColor(blackColor);
- /* ---------- */
- maskPercent = 99; /* Initialize some stuff ... */
- tempX = (windRect.right - windRect.left) / 2;
- tempY = (windRect.bottom - windRect.top) / 2;
- ;
- while (maskPercent >= 0) {
-
- irisRect = windRect;
- InsetRect(&irisRect,
- (tempX * maskPercent) / 100,
- (tempY * maskPercent) / 100);
- irisRgn = NewRgn();
- OpenRgn();
- {
- FrameRoundRect(&irisRect,
- ((irisRect.right - irisRect.left) * maskPercent) / 100,
- ((irisRect.bottom - irisRect.top) * maskPercent) / 100);
- }
- CloseRgn(irisRgn);
-
- SectRgn(irisRgn, ((**bragOSHdl).origPort)->visRgn, irisRgn);
- CopyBits(
- (**bragOSHdl).offBitMapPtr,
- &( ((**bragOSHdl).origPort)->portBits ),
- &bragRect,
- &( (**bragOSHdl).drawingRect ),
- srcCopy,
- irisRgn
- );
-
- DisposeRgn(irisRgn);
- ;
- if (gMac2) {
- if (gColorDepth = 1)
- Delay(4, &finalTicks); /* Black-and-white too doggone fast !! */
- else
- Delay(2, &finalTicks); /* Color a tad better. */
- }
- else
- Delay(1, &finalTicks);
- ;
- maskPercent = maskPercent - 1;
- } /* while maskPercent >= 0 */
- /* ---------- */
-
- byebye:
- DisposOffScreen(&bragOSHdl);
-
- SetWindowPic(bragging, centeredPic); /* ... for updating time. */
-
- // DisposeWindow(bragging); /* Leave it be !! */
-
- SysBeep(10); /* Wake Up Call !! */
-
- } /* DoSpiffyIris */
-
-
-
- void CenterRects (Rect *srcRect, Rect *withinRect) {
-
- short srcW, srcH, tempX, tempY;
-
-
- srcW = srcRect->right - srcRect->left;
- // within width - source width:
- tempX = withinRect->right - withinRect->left - srcW;
- srcRect->left = withinRect->left + tempX/2;
- srcRect->right = srcRect->left + srcW;
- ;
- srcH = srcRect->bottom - srcRect->top;
- // within height - source height:
- tempY = withinRect->bottom - withinRect->top - srcH;
- srcRect->top = withinRect->top + tempY/2;
- srcRect->bottom = srcRect->top + srcH;
-
- } /* CenterRects */
-
-
-
- Boolean cmpAlpha (char in, char with) {
- /* If the input is NOT a letter, then we advance to the NEXT available letter. */
-
- Boolean result = false;
-
-
- if ( !isalpha(with) ) SysBeep(10);
- else {
-
- if ( !isalpha(in) ) in = nextAlpha(in);
- ;
- if ( isupper(with) && islower(in) )
- in = toupper(in);
- else if ( islower(with) && isupper(in) )
- in = tolower(in);
- ;
- if (in == with) result = true;
-
- } /* end: with isalpha */
-
- return (result);
-
- } /* cmpAlpha */
-
-
-
- char nextAlpha (char start) {
-
- char next;
-
-
- next = (char) (start + 1);
-
- if ( !isalpha(next) ) {
- if ((next < 'A') || (next > 'z')) next = 'A';
- else /* inbetween */ next = 'a';
- }
-
- return (next);
-
- } /* nextAlpha */
-
-
-
-
- /* { end file = "misc.c" } */